package com.hjm.ui.home;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.hjm.ui.R;
import com.hjm.ui.home.adapter.HomeGoodsItemAdapter;
import com.hjm.ui.home.entity.Goods;

import java.util.List;

public class HomeGoodsItem extends LinearLayout{
    private RecyclerView homeGoodsRecyclerView;
    List<? extends Goods> goodsList;
    HomeGoodsItemAdapter homeGoodsItemAdapter;
    private HomeGoodsItemAdapter.Listener listener;
    private String TAG = "HomeGoodsItem";

    public HomeGoodsItem(Context context) {
        super(context);
        init();
    }

    public HomeGoodsItem(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public HomeGoodsItem(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    void init(){
        LayoutInflater.from(getContext()).inflate(R.layout.layout_home_item, this, true);
        homeGoodsRecyclerView = findViewById(R.id.recycler_view_home_goods);
        // 设置不能滑动
        homeGoodsRecyclerView.setNestedScrollingEnabled(false);
    }
    public <T extends Goods> void setData(List<T> goodsList){
        Log.d(TAG, "setData: 获取到数据 "+goodsList.size());
        this.goodsList = goodsList;
        if (homeGoodsItemAdapter == null){
            // 两列
            homeGoodsRecyclerView.setLayoutManager(new GridLayoutManager(getContext(), 2));
            homeGoodsItemAdapter = new HomeGoodsItemAdapter(this.goodsList, listener,getContext());
            homeGoodsRecyclerView.setAdapter(homeGoodsItemAdapter);
        }
    }
    public void setListener(HomeGoodsItemAdapter.Listener listener){
        this.listener = listener;
    }
}
